/*     */ package com.zimbra.cs.gal;
/*     */ 
/*     */ import com.zimbra.common.account.ZAttrProvisioning.GalMode;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.gal.GalOp;
/*     */ import com.zimbra.cs.account.ldap.LdapGalMapRules;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapDomain;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ 
/*     */ public class GalSearchConfig
/*     */ {
/*     */   protected GalOp mOp;
/*     */   protected GalType mGalType;
/*     */   protected String[] mUrl;
/*     */   protected boolean mStartTlsEnabled;
/*     */   protected String mFilter;
/*     */   protected String mSearchBase;
/*     */   protected String mAuthMech;
/*     */   protected String mBindDn;
/*     */   protected String mBindPassword;
/*     */   protected String mKerberosPrincipal;
/*     */   protected String mKerberosKeytab;
/*     */   protected String mTimestampFormat;
/*     */   protected String mTokenizeKey;
/*     */   protected int mPageSize;
/*     */   protected LdapGalMapRules mRules;
/*     */   
/*     */   public static enum GalType
/*     */   {
/*  36 */     zimbra, 
/*  37 */     ldap;
/*     */     
/*     */     private GalType() {}
/*     */     
/*  41 */     public static GalType fromString(String s) throws ServiceException { try { return valueOf(s);
/*     */       } catch (IllegalArgumentException e) {
/*  43 */         throw ServiceException.INVALID_REQUEST("unknown gal type: " + s, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static GalSearchConfig create(Domain domain, GalOp op, GalType type, GalSearchType stype) throws ServiceException
/*     */   {
/*  50 */     switch (type) {
/*     */     case zimbra: 
/*  52 */       return new ZimbraConfig(domain, op, stype);
/*     */     case ldap: 
/*  54 */       return new LdapConfig(domain, op);
/*     */     }
/*  56 */     return null;
/*     */   }
/*     */   
/*     */   public static String fixupExternalGalSearchBase(String searchBase) {
/*  60 */     if ((searchBase == null) || ("ROOT".equals(searchBase))) {
/*  61 */       return "";
/*     */     }
/*  63 */     return searchBase;
/*     */   }
/*     */   
/*     */   public static GalSearchConfig create(DataSource ds) throws ServiceException
/*     */   {
/*  68 */     return new DataSourceConfig(ds);
/*     */   }
/*     */   
/*     */   private static class ZimbraConfig extends GalSearchConfig {
/*     */     public ZimbraConfig(Domain domain, GalOp op, GalSearchType stype) throws ServiceException {
/*  73 */       loadZimbraConfig(domain, op, stype);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class LdapConfig extends GalSearchConfig {
/*  78 */     public LdapConfig(Domain domain, GalOp op) throws ServiceException { loadConfig(domain, op); }
/*     */   }
/*     */   
/*     */   private static class DataSourceConfig extends GalSearchConfig
/*     */   {
/*     */     public DataSourceConfig(DataSource ds) throws ServiceException {
/*  84 */       this.mGalType = GalSearchConfig.GalType.fromString(ds.getAttr("zimbraGalType"));
/*  85 */       Domain domain = Provisioning.getInstance().getDomain(ds.getAccount());
/*  86 */       if (this.mGalType == GalSearchConfig.GalType.zimbra) {
/*  87 */         loadZimbraConfig(domain, GalOp.sync, GalSearchType.all);
/*     */       } else {
/*  89 */         loadConfig(domain, GalOp.sync);
/*  90 */         if ((this.mUrl.length == 0) || (this.mFilter == null))
/*  91 */           loadConfig(domain, GalOp.search);
/*  92 */         String[] url = ds.getMultiAttr("zimbraGalSyncLdapURL");
/*  93 */         if ((url != null) && (url.length > 0))
/*  94 */           this.mUrl = url;
/*  95 */         this.mFilter = ds.getAttr("zimbraGalSyncLdapFilter", this.mFilter);
/*  96 */         this.mSearchBase = ds.getAttr("zimbraGalSyncLdapSearchBase", this.mSearchBase);
/*  97 */         this.mStartTlsEnabled = ds.getBooleanAttr("zimbraGalSyncLdapStartTlsEnabled", this.mStartTlsEnabled);
/*  98 */         this.mAuthMech = ds.getAttr("zimbraGalSyncLdapAuthMech", this.mAuthMech);
/*  99 */         this.mBindDn = ds.getAttr("zimbraGalSyncLdapBindDn", this.mBindDn);
/* 100 */         this.mBindPassword = ds.getAttr("zimbraGalSyncLdapBindPassword", this.mBindPassword);
/* 101 */         this.mKerberosPrincipal = ds.getAttr("zimbraGalSyncLdapKerberos5Principal", this.mKerberosPrincipal);
/* 102 */         this.mKerberosKeytab = ds.getAttr("zimbraGalSyncLdapKerberos5Keytab", this.mKerberosKeytab);
/* 103 */         this.mTimestampFormat = ds.getAttr("zimbraGalSyncTimestampFormat", this.mTimestampFormat);
/* 104 */         this.mPageSize = ds.getIntAttr("zimbraGalSyncLdapPageSize", this.mPageSize);
/*     */       }
/* 106 */       String[] attrs = ds.getMultiAttr("zimbraGalLdapAttrMap");
/* 107 */       String[] valueMap = ds.getMultiAttr("zimbraGalLdapValueMap");
/* 108 */       String groupHandlerClass = ds.getAttr("zimbraGalLdapGroupHandlerClass");
/* 109 */       if ((attrs.length > 0) || (valueMap.length > 0) || (groupHandlerClass != null)) {
/* 110 */         if (attrs.length == 0)
/* 111 */           attrs = domain.getMultiAttr("zimbraGalLdapAttrMap");
/* 112 */         if (valueMap.length == 0)
/* 113 */           valueMap = domain.getMultiAttr("zimbraGalLdapValueMap");
/* 114 */         if (groupHandlerClass == null)
/* 115 */           groupHandlerClass = domain.getAttr("zimbraGalLdapGroupHandlerClass");
/* 116 */         this.mRules = new LdapGalMapRules(attrs, valueMap, groupHandlerClass);
/*     */       }
/*     */       
/* 119 */       if (StringUtil.isNullOrEmpty(this.mFilter)) {
/* 120 */         throw ServiceException.INVALID_REQUEST("missing GAL filter", null);
/*     */       }
/* 122 */       this.mFilter = com.zimbra.cs.account.gal.GalUtil.expandFilter(null, this.mFilter, "", null);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void loadZimbraConfig(Domain domain, GalOp op, GalSearchType stype) throws ServiceException
/*     */   {
/* 128 */     this.mRules = new LdapGalMapRules(domain, true);
/* 129 */     this.mOp = op;
/* 130 */     GalFilter.NamedFilter filterName = null;
/*     */     
/* 132 */     switch (op) {
/*     */     case sync: 
/* 134 */       filterName = stype == GalSearchType.group ? GalFilter.NamedFilter.zimbraGroupSync : stype == GalSearchType.resource ? GalFilter.NamedFilter.zimbraResourceSync : stype == GalSearchType.all ? GalFilter.NamedFilter.zimbraSync : GalFilter.NamedFilter.zimbraAccountSync;
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 139 */       break;
/*     */     case search: 
/* 141 */       filterName = stype == GalSearchType.group ? GalFilter.NamedFilter.zimbraGroups : stype == GalSearchType.resource ? GalFilter.NamedFilter.zimbraResources : stype == GalSearchType.all ? GalFilter.NamedFilter.zimbraSearch : GalFilter.NamedFilter.zimbraAccounts;
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 146 */       this.mTokenizeKey = domain.getAttr("zimbraGalTokenizeSearchKey", null);
/* 147 */       break;
/*     */     case autocomplete: 
/* 149 */       filterName = stype == GalSearchType.group ? GalFilter.NamedFilter.zimbraGroupAutoComplete : stype == GalSearchType.resource ? GalFilter.NamedFilter.zimbraResourceAutoComplete : stype == GalSearchType.all ? GalFilter.NamedFilter.zimbraAutoComplete : GalFilter.NamedFilter.zimbraAccountAutoComplete;
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 154 */       this.mTokenizeKey = domain.getAttr("zimbraGalTokenizeAutoCompleteKey", null);
/*     */     }
/*     */     
/*     */     
/* 158 */     String filter = null;
/* 159 */     if (filterName != null) {
/* 160 */       filter = getFilterDef(filterName);
/*     */     }
/*     */     
/* 163 */     if ((filter == null) && (op == GalOp.sync)) {
/* 164 */       filter = "(&(|(objectclass=zimbraAccount)(objectclass=zimbraDistributionList)(objectclass=zimbraGroup))(!(&(objectclass=zimbraCalendarResource)(!(zimbraAccountStatus=active)))))";
/*     */     }
/*     */     
/* 167 */     this.mAuthMech = "simple";
/*     */     
/* 169 */     if (filter == null) {
/* 170 */       filter = "";
/* 171 */     } else if (!filter.startsWith("(")) {
/* 172 */       filter = "(" + filter + ")";
/*     */     }
/*     */     
/* 175 */     String dnSubtreeMatchFilter = null;
/* 176 */     String searchBaseRaw = ZimbraGalSearchBase.getSearchBaseRaw(domain, op);
/* 177 */     if (ZimbraGalSearchBase.PredefinedSearchBase.DOMAIN.name().equals(searchBaseRaw)) {
/* 178 */       dnSubtreeMatchFilter = ((LdapDomain)domain).getDnSubtreeMatchFilter().toFilterString();
/*     */     }
/*     */     
/* 181 */     if (dnSubtreeMatchFilter == null) {
/* 182 */       dnSubtreeMatchFilter = "";
/*     */     }
/* 184 */     this.mFilter = ("(&" + filter + "(!(zimbraHideInGal=TRUE))(!(zimbraIsSystemResource=TRUE))" + dnSubtreeMatchFilter + ")");
/*     */     
/* 186 */     this.mSearchBase = ZimbraGalSearchBase.getSearchBase(domain, op);
/* 187 */     this.mGalType = GalType.zimbra;
/* 188 */     this.mTimestampFormat = "yyyyMMddHHmmss'Z'";
/* 189 */     this.mPageSize = 1000;
/*     */   }
/*     */   
/*     */   private boolean isConfigComplete() {
/* 193 */     return (this.mUrl.length > 0) && (this.mFilter != null) && ((this.mAuthMech.equals("none")) || ((this.mAuthMech.equals("simple")) && (this.mBindDn != null) && (this.mBindPassword != null)) || ((this.mAuthMech.equals("kerberos5")) && (this.mKerberosKeytab != null) && (this.mKerberosPrincipal != null)));
/*     */   }
/*     */   
/*     */ 
/*     */   protected void loadConfig(Domain domain, GalOp op)
/*     */     throws ServiceException
/*     */   {
/* 200 */     this.mRules = new LdapGalMapRules(domain, false);
/* 201 */     this.mOp = op;
/*     */     
/* 203 */     ZAttrProvisioning.GalMode galMode = domain.getGalMode();
/* 204 */     if (galMode == ZAttrProvisioning.GalMode.zimbra) {
/* 205 */       loadZimbraConfig(domain, op, null);
/*     */     }
/* 207 */     switch (op) {
/*     */     case sync: 
/* 209 */       this.mUrl = domain.getMultiAttr("zimbraGalSyncLdapURL");
/* 210 */       this.mFilter = domain.getAttr("zimbraGalLdapFilter");
/* 211 */       this.mSearchBase = domain.getAttr("zimbraGalSyncLdapSearchBase", "");
/* 212 */       this.mStartTlsEnabled = domain.getBooleanAttr("zimbraGalSyncLdapStartTlsEnabled", false);
/* 213 */       this.mAuthMech = domain.getAttr("zimbraGalSyncLdapAuthMech", "simple");
/* 214 */       this.mBindDn = domain.getAttr("zimbraGalSyncLdapBindDn");
/* 215 */       this.mBindPassword = domain.getAttr("zimbraGalSyncLdapBindPassword");
/* 216 */       this.mKerberosPrincipal = domain.getAttr("zimbraGalSyncLdapKerberos5Principal");
/* 217 */       this.mKerberosKeytab = domain.getAttr("zimbraGalSyncLdapKerberos5Keytab");
/* 218 */       this.mTimestampFormat = domain.getAttr("zimbraGalSyncTimestampFormat", "yyyyMMddHHmmss'Z'");
/* 219 */       this.mPageSize = domain.getIntAttr("zimbraGalSyncLdapPageSize", 1000);
/* 220 */       if (isConfigComplete())
/*     */         break;
/*     */     case search: 
/* 223 */       this.mUrl = domain.getMultiAttr("zimbraGalLdapURL");
/* 224 */       this.mFilter = domain.getAttr("zimbraGalLdapFilter");
/* 225 */       this.mSearchBase = domain.getAttr("zimbraGalLdapSearchBase", "");
/* 226 */       this.mStartTlsEnabled = domain.getBooleanAttr("zimbraGalLdapStartTlsEnabled", false);
/* 227 */       this.mAuthMech = domain.getAttr("zimbraGalLdapAuthMech", "simple");
/* 228 */       this.mBindDn = domain.getAttr("zimbraGalLdapBindDn");
/* 229 */       this.mBindPassword = domain.getAttr("zimbraGalLdapBindPassword");
/* 230 */       this.mKerberosPrincipal = domain.getAttr("zimbraGalLdapKerberos5Principal");
/* 231 */       this.mKerberosKeytab = domain.getAttr("zimbraGalLdapKerberos5Keytab");
/* 232 */       this.mTokenizeKey = domain.getAttr("zimbraGalTokenizeSearchKey");
/* 233 */       this.mPageSize = domain.getIntAttr("zimbraGalLdapPageSize", 1000);
/* 234 */       break;
/*     */     case autocomplete: 
/* 236 */       this.mUrl = domain.getMultiAttr("zimbraGalLdapURL");
/* 237 */       this.mFilter = domain.getAttr("zimbraGalAutoCompleteLdapFilter");
/* 238 */       this.mSearchBase = domain.getAttr("zimbraGalLdapSearchBase", "");
/* 239 */       this.mStartTlsEnabled = domain.getBooleanAttr("zimbraGalLdapStartTlsEnabled", false);
/* 240 */       this.mAuthMech = domain.getAttr("zimbraGalLdapAuthMech", "simple");
/* 241 */       this.mBindDn = domain.getAttr("zimbraGalLdapBindDn");
/* 242 */       this.mBindPassword = domain.getAttr("zimbraGalLdapBindPassword");
/* 243 */       this.mKerberosPrincipal = domain.getAttr("zimbraGalLdapKerberos5Principal");
/* 244 */       this.mKerberosKeytab = domain.getAttr("zimbraGalLdapKerberos5Keytab");
/* 245 */       this.mTokenizeKey = domain.getAttr("zimbraGalTokenizeAutoCompleteKey");
/* 246 */       this.mPageSize = domain.getIntAttr("zimbraGalLdapPageSize", 1000);
/*     */     }
/*     */     
/* 249 */     if ((this.mFilter != null) && (this.mFilter.indexOf("(") == -1))
/* 250 */       this.mFilter = getFilterDef(this.mFilter);
/* 251 */     this.mGalType = GalType.ldap;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public GalOp getOp()
/*     */   {
/* 271 */     return this.mOp;
/*     */   }
/*     */   
/* 274 */   public GalType getGalType() { return this.mGalType; }
/*     */   
/*     */   public String[] getUrl() {
/* 277 */     return this.mUrl;
/*     */   }
/*     */   
/* 280 */   public boolean getStartTlsEnabled() { return this.mStartTlsEnabled; }
/*     */   
/*     */   public String getFilter() {
/* 283 */     return this.mFilter;
/*     */   }
/*     */   
/* 286 */   public String getSearchBase() { return fixupExternalGalSearchBase(this.mSearchBase); }
/*     */   
/*     */   public String getAuthMech() {
/* 289 */     return this.mAuthMech;
/*     */   }
/*     */   
/* 292 */   public String getBindDn() { return this.mBindDn; }
/*     */   
/*     */   public String getBindPassword() {
/* 295 */     return this.mBindPassword;
/*     */   }
/*     */   
/* 298 */   public String getKerberosPrincipal() { return this.mKerberosPrincipal; }
/*     */   
/*     */   public String getKerberosKeytab() {
/* 301 */     return this.mKerberosKeytab;
/*     */   }
/*     */   
/* 304 */   public int getPageSize() { return this.mPageSize; }
/*     */   
/*     */   public LdapGalMapRules getRules() {
/* 307 */     return this.mRules;
/*     */   }
/*     */   
/* 310 */   public String getTokenizeKey() { return this.mTokenizeKey; }
/*     */   
/*     */   public void setGalType(GalType galType) {
/* 313 */     this.mGalType = galType;
/*     */   }
/*     */   
/* 316 */   public void setUrl(String[] url) { this.mUrl = url; }
/*     */   
/*     */   public void setStartTlsEnabled(boolean startTlsEnabled) {
/* 319 */     this.mStartTlsEnabled = startTlsEnabled;
/*     */   }
/*     */   
/* 322 */   public void setFilter(String filter) { this.mFilter = filter; }
/*     */   
/*     */   public void setSearchBase(String searchBase) {
/* 325 */     this.mSearchBase = searchBase;
/*     */   }
/*     */   
/* 328 */   public void setAuthMech(String authMech) { this.mAuthMech = authMech; }
/*     */   
/*     */   public void setBindDn(String bindDn) {
/* 331 */     this.mBindDn = bindDn;
/*     */   }
/*     */   
/* 334 */   public void setBindPassword(String bindPassword) { this.mBindPassword = bindPassword; }
/*     */   
/*     */   public void setKerberosPrincipal(String kerberosPrincipal) {
/* 337 */     this.mKerberosPrincipal = kerberosPrincipal;
/*     */   }
/*     */   
/* 340 */   public void setKerberosKeytab(String kerberosKeytab) { this.mKerberosKeytab = kerberosKeytab; }
/*     */   
/*     */   public void setRules(LdapGalMapRules rules) {
/* 343 */     this.mRules = rules;
/*     */   }
/*     */   
/* 346 */   public void setTokenizeKey(String tokenizeKey) { this.mTokenizeKey = tokenizeKey; }
/*     */   
/*     */   public static String getFilterDef(GalFilter.NamedFilter filter) throws ServiceException
/*     */   {
/* 350 */     return getFilterDef(filter.name());
/*     */   }
/*     */   
/*     */   public static String getFilterDef(String name) throws ServiceException {
/* 354 */     String[] queryExprs = Provisioning.getInstance().getConfig().getMultiAttr("zimbraGalLdapFilterDef");
/* 355 */     String fname = name + ":";
/* 356 */     String queryExpr = null;
/* 357 */     for (int i = 0; i < queryExprs.length; i++) {
/* 358 */       if (queryExprs[i].startsWith(fname)) {
/* 359 */         queryExpr = queryExprs[i].substring(fname.length());
/*     */       }
/*     */     }
/*     */     
/* 363 */     if (queryExpr == null) {
/* 364 */       com.zimbra.common.util.ZimbraLog.gal.warn("missing filter def " + name + " in " + "zimbraGalLdapFilterDef");
/*     */     }
/* 366 */     return queryExpr;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/gal/GalSearchConfig.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */